// pages/draw/drawList/index.js
var common = require("../../../public.js");
var timer;
Page({
    /**
     * 页面的初始数据
     */
    data: {
        showRedPoint:false,
      isOverShare: true, //全局分享标识请勿删除
        ffff: false,
        pageSize: 10000,
        pageNum: 1,
        drawList: [],
        drawAwardStr: '',
        canLoad: true,
        noMoreData: false,
        isLoading: false,
        time: 30 * 60 * 60 * 1000,
        timerr: '',
        selected: 1,
        // personNum: 0, //时间显示
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onShow() {
        clearInterval(this.data.timerr)
        var that = this
        console.log('cccc');
        this.getDrawList(true)
        // this.needTokenAndMessageList();
        // common.getLocalUserInfo(function (success, data) {
        //     if (success) {
        //         console.log('ddddd');
        //         that.setData({
        //             userInfo: data.user,
        //             token: data.token
        //         }, function () {
        //             console.log('fffff');
        //             console.log('aaaaaa',this.data.userInfo.id);
        //             this.getDrawList(true)
        //         })
        //     }else{
        //         console.log('eeeee');
        //     }
        // },true)
      
        // this.getStoreDrawAwardList()
    },
    onLoad: function (options) {
        console.log('bbbbb');
        // this.getDrawList(true)
        // this.getStoreDrawAwardList()
    },
    getStoreDrawAwardList() {
        const that = this
        let params = {};
        common.post("/storeDraw/getStoreDrawAwardList", params, function (res) {
            if (res.status === 200) {
                let str = res.data.toString();
                let str_arr = str.replace(new RegExp(",", ("gm")), '               ');

                that.setData({
                    drawAwardStr: str_arr
                })
            }
        }, function () {

        })
    },
    //   列表
    getDrawList(refresh = false) {

        const that = this
        const pageNum = refresh ? 1 : that.data.pageNum + 1
        const params = {
            pageNum:1,
            // userId:that.data.userInfo.id,
            pageSize: that.data.pageSize
        }
        this.setData({
            isLoading: true
        })
        common.post("/storeDraw/getStoreDrawList", params, function (res) {
            if (res.status === 200) {
                var nowDate = new Date().getTime() //现在时间戳
                res.data.forEach((item, index) => {
                    // item.awardTime = 1680586795000

                    // 到了开奖时间，参与人员不够
                    if ((nowDate - item.awardTime > 0) && (item.applyNum < item.personNums)) {  //超过开奖时间
                        item.personNum = 0
                        item.timers = ''
                        // 到了开奖时间，参与人员够了
                    } else if ((nowDate - item.awardTime > 0) && (item.applyNum >= item.personNums)) {
                        item.personNum = 1
                        item.timers = ''
                    } else if (nowDate - item.awardTime <= 0) {
                        var timeCha = item.awardTime - nowDate
                        var ttt = timeCha / 1000

                        that.setTimeFn(ttt, index)

                        item.timers = item.awardTime - nowDate
                        item.personNum = 2
                        // 未到开奖时间
                    } else {
                        item.personNum = ''
                    }
                })
                if (refresh === false) {
                    if (res.data == '') {
                        that.setData({
                            noMoreData: true,
                            canLoad: false
                        })
                    } else {
                        that.setData({
                            // drawList: [...that.data.drawList, ...res.data].reverse(),
                            drawList: [...that.data.drawList, ...res.data],
                            // pageNum
                        })
                    }
                } else {
                    that.setData({
                        // drawList: res.data.reverse(),
                        drawList: res.data,
                        // pageNum
                    })
                }
                console.log(res.data, "555555555")
            }
        }, function () {
            that.setData({
                isLoading: false
            })

        })
    },

    setTimeFn(ttt, index) {
        const that = this
        let timerr = setInterval(() => {
            ttt--
            // console.log(ttt, "666666")
            if (ttt < 0) {
                // this.getDrawList(true)
                // console.log("测试数据66666666666666666666")
                clearInterval(timerr)
                this.getDrawList(true)
            } else {

            }
        }, 1000)



    },

    toDetail(e) {
        let {
            id
        } = e.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/draw/index?drawId=' + id,
        })
    },
    onReachBottom: function () {
        if (this.data.canLoad) {
            this.getDrawList(false)
        }
    },
    loadMore() {
        if (this.data.canLoad) {
            this.getDrawList(false)
        }
    },
    onShareAppMessage: function () {
    },
    onUnload() {
        console.log("离开此页面")
        clearInterval(this.data.timerr)
        console.log(this.data.timerr)
    },
    onHide() {

        clearInterval(this.data.timerr)
    },
    onShareAppMessage: function () {
        return {
            title: '抽奖得好礼，金豆送不停，飞羊邀你来领福利啦！',
            path: 'pages/draw/drawList/index',
            // imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/yearCard/year_cardshare.jpg',

            success: (res) => { },
            fail: (res) => {
                // 分享失败
            }
        }
    },
    onShareTimeline() {
        return {
            title: '抽奖得好礼，金豆送不停，飞羊邀你来领福利啦！',
            path: 'pages/draw/drawList/index',
            // imageUrl: 'http://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/yearCard/year_cardshare.jpg',
            success: (res) => { },
            fail: (res) => {
                // 分享失败
            }
        }
    },
    needTokenAndMessageList(){
        
        var that = this
    common.getLocalUserInfo(function (success, data) {
      if (success) {
        that.setData({
          userInfo: data.user,
          token: data.token
        }, function () {
          that.mess_fn();
          // wx.hideTabBarRedDot({
          //   index: 3,
          // })
        })
      } else {
        that.setData({
          userInfo: null,
          token: '',
        })
      }
    })
    },

    mess_fn() {
        var that = this
        var tmp = {
          userId: that.data.userInfo.id,
          messageType: 10,
          token: that.data.token,
          pageNum: that.data.pageNum,
          pageSize: 10
        }
        common.get('/userMessage/getUserMessage', tmp, res => {
          if (res.status === 200) {
              var list = res.data;
          
         
       
            that.setData({
              showRedPoint: list.length>0?false:true,
       
              
            },function(){
                // that.setData({
                //     nianList: list<10,
                    
                //   })
            })
          } else {
            common.showRequestErrMsg(res)
          }
        })
      },
})